package com.nolon.flange.viewmodel

import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import android.view.View
import androidx.databinding.ObservableField
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import androidx.preference.PreferenceManager
import com.nolon.flange.R
import com.nolon.flange.extensions.toast
import n.Init
import n.Mediator
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class UserViewModel(application: Application) :
    AndroidViewModel(application) {
    private val SECRET_KEY = "asdfghjklqdfadfdwertyu"
    private val IV = "1234567899876543"
    private var sp: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(application)

    fun init(c: Context) {
//         检查是否已经激活
        if (Init.isActive(c)) {
            // 如果已经激活，处理相关显示和禁用组件
            sn.set(Init.n0020(c))
            isActiveBtnEnable.set(false)
            isActiveEditEnable.set(false)
            activeCode.set(Init.n0040(c))
            activationMessage.set(c.getString(R.string.activated))
        } else {
            viewModelScope.launch {
                val strSn = withContext(Dispatchers.IO) {
                    val code2 = Mediator.n0010(c)
                    val code3 = Mediator.n0011(c)
                    val code4 = Mediator.n0012(c)
                    val code5 = Mediator.n0013(c)
                    val code6 = Mediator.n0014(c)
                    val code7 = Mediator.n0015(c)
                    val code = Mediator.n0016(c)   //只有这个是工作的
                    code + code2 + code3 + code4 + code5 + code6 + code7
                }
                sn.set(strSn)
            }
        }
        // 还没有激活
        // 界面正常使用
    }

    // sn
    val sn: ObservableField<String> = ObservableField("")

    // active code
    val isActiveEditEnable: ObservableField<Boolean> = ObservableField(true)
    val activeCode: ObservableField<String> = ObservableField("")

    // active button
    val isActiveBtnEnable: ObservableField<Boolean> = ObservableField(true)

    val activationMessage: ObservableField<String> =
        ObservableField(application.getString(R.string.not_activated))

    fun active(v: View) {
        val strSn = sn.get()
        if (strSn.isNullOrEmpty()) {
            return
        }
        val result = Mediator.n0880(v.context, activeCode.get() ?: "")
        toast(v.context, "比对结果：$result！")
        if (result) {
            disableViews(v)
        }
    }

    fun genActive(v: View): Unit {
        if (activeCode.get().isNullOrEmpty()) {
            val deCode = Mediator.n0006(sn.get() ?: "")
            activeCode.set(deCode)
            toast(v.context, "已生成激活码")
        }
    }

    private fun disableViews(v: View): Unit {
        isActiveBtnEnable.set(false)
        isActiveEditEnable.set(false)
        activationMessage.set(v.context.getString(R.string.activated))
    }

}